﻿using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace Notepad_X
{
    static class Program
    {
        public static bool notSingleInstance;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
      
       static void Main(string[] args)
       {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            bool ok = true;
            notSingleInstance = !System.IO.File.Exists(Application.StartupPath + "\\notSingleInstance");
            //Checks if ICSharpCode.TextEditor.dll is missing
            if (!System.IO.File.Exists(Application.StartupPath + "\\ICSharpCode.TextEditor.dll"))
            {
                if (MessageBox.Show("ICSharpCode.TextEditor.dll is missing, Notepad X will try to download it automaticly contiue or you must fix it manually", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Download dll = new Download("http://notepad-x.sourceforge.net/files/ICSharpCode.TextEditor.dll", Application.StartupPath + "//ICSharpCode.TextEditor.dll", DownloadTypes.Autoclose);
                    dll.ShowDialog(null);
                }
                else
                {
                    ok = false;
                }
            }

            if (ok)
            {
                App myApp = new App();
                myApp.Run(args);

                //Application.Run(new Main()); // to remove
            }
            else
            {
                Application.Exit();
            }
        }

        class App : WindowsFormsApplicationBase
        {
            public App()
            {
                // Make this a single-instance application
                this.IsSingleInstance = Program.notSingleInstance;
                this.EnableVisualStyles = true;

                this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses;

                // Add StartupNextInstance handler
                this.StartupNextInstance += new StartupNextInstanceEventHandler(this.SIApp_StartupNextInstance);
            }

            /// <summary>
            /// We are responsible for creating the application's main form in this override.
            /// </summary>
            protected override void OnCreateMainForm()
            {
                // Create an instance of the main form and set it in the application; 
                // but don't try to run it.
                this.MainForm = new Main();

                // We want to pass along the command-line arguments to this first instance

                // Allocate room in our string array
                ((Main)this.MainForm).Args = new string[this.CommandLineArgs.Count];

                // And copy the arguments over to our form
                this.CommandLineArgs.CopyTo(((Main)this.MainForm).Args, 0);
            }

            /// <summary>
            /// This is called for additional instances. The application model will call this 
            /// function, and terminate the additional instance when this returns.
            /// </summary>
            /// <param name="eventArgs"></param>
            protected void SIApp_StartupNextInstance(object sender,StartupNextInstanceEventArgs eventArgs)
            {
                // Copy the arguments to a string array
                string[] args = new string[eventArgs.CommandLine.Count];
                eventArgs.CommandLine.CopyTo(args, 0);

                // Create an argument array for the Invoke method
                object[] parameters = new object[2];
                parameters[0] = this.MainForm;
                parameters[1] = args;

                // Need to use invoke to b/c this is being called from another thread.
                this.MainForm.Invoke(new Main.ProcessParametersDelegate(((Main)this.MainForm).ProcessParameters),parameters);
                this.MainForm.BringToFront();
            }
        }
    }
}
